package com.sskj.community.ui.fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.community.R2;
import com.sskj.community.presenter.PublishPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseFragment;

import com.sskj.community.R;
import com.sskj.lib.bean.NoticeBean;
import com.sskj.lib.widget.BottomDialog;
import com.zzhoujay.richtext.RichText;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/06/25
 */
@Route(path = RConfig.COMMUNITY_FRAGMENT_PUBLISH)
public class PublishFragment extends BaseFragment<PublishPresenter> {

    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    private BaseAdapter<NoticeBean.ListBean> adapter;
    private int p = 1;
    private String size = "10";

    @Override
    public int getLayoutId() {
        return R.layout.community_fragment_publish;
    }

    @Override
    public PublishPresenter getPresenter() {
        return new PublishPresenter();
    }

    @Override
    public void initView() {
        mPresenter.getPublishData(true, p, size);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setLastDraw(true)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(Color.TRANSPARENT));
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            p = 1;
            mPresenter.getPublishData(false, p, size);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            p++;
            mPresenter.getPublishData(false, p, size);
        });
    }

    @Override
    public void initData() {
        adapter = new BaseAdapter<NoticeBean.ListBean>(R.layout.community_item_publish, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, NoticeBean.ListBean item) {
                ConstraintLayout clAll = holder.getView(R.id.clAll);
                TextView content = holder.getView(R.id.tv_content);
                holder.setText(R.id.tv_title, item.getTitle())
                        .setText(R.id.tv_date, TimeFormatUtil.formatE(Long.valueOf(item.getCreate_time()) * 1000));
                RichText.from(item.getContent()).into(content);
                clAll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ARouter.getInstance().build(RConfig.COMMUNITY_ACTIVITY_PUBLISH_ARTICLE).withString("id", item.getId()).navigation();
                    }
                });
            }
        };
//        ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
//            @Override
//            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
//                if (adapter.getData().size() == 0) {
//                    return;
//                }
//                ARouter.getInstance().build(RConfig.COMMUNITY_ACTIVITY_PUBLISH_ARTICLE)
//                        .withString("id", adapter.getData().get(position).getId())
//                        .navigation();
//            }
//        });
    }


    @SuppressLint("CheckResult")
    public void getNoticeListSuccess(NoticeBean data) {
        if (p == 1) {
            adapter.setNewData(data.getList());
        } else {
            adapter.addData(data.getList());
        }
    }

    public void stopRefresh() {
        if (smartRefreshView != null) {
            smartRefreshView.finishRefresh();
            smartRefreshView.finishLoadMore();
        }
    }
}
